﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using IronSuit.Utility;
using MySql.Data.MySqlClient;

namespace IronSuit.DataMap
{
	public abstract class DataHandler
	{
		public abstract string TableName
		{
			get;
			set;
		}
		public virtual List<string> FieldList
		{
			get
			{
				return null;
			}
		}
		protected internal List<string> KeyFieldList = new List<string>();
		protected  DataEntity Entity;
		public DataHandler(DataEntity entity)
		{
			Entity = entity;
		}

		protected internal string GetQuerySql(out List<SqlParameter> ParaList)
		{
			ParaList = new List<SqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Select * from " + TableName + " where ");
			foreach (string KeyField in KeyFieldList)
			{
				SqlSB.Append(KeyField + "=@" + KeyField);
				if (Entity.FieldValueDic.ContainsKey(KeyField))
				{
					SqlParameter SP = new SqlParameter("@" + KeyField, Entity.FieldValueDic[KeyField]);
					ParaList.Add(SP);
				}
				else
					throw new ISException("数据库查询缺少主键!");
			}
			return SqlSB.ToString();
		}
		protected internal string GetQuerySql(out List<MySqlParameter> ParaList)
		{
			ParaList = new List<MySqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Select * from " + TableName + " where ");
			foreach (string KeyField in KeyFieldList)
			{
				SqlSB.Append(KeyField + "=@" + KeyField);
				if (Entity.FieldValueDic.ContainsKey(KeyField))
				{
					MySqlParameter SP = new MySqlParameter("@" + KeyField, Entity.FieldValueDic[KeyField]);
					ParaList.Add(SP);
				}
				else
					throw new ISException("数据库查询缺少主键!");
			}
			return SqlSB.ToString();
		}
		protected internal string GetQueryListSql(out List<MySqlParameter> ParaList,int pageSize)
		{
			ParaList = new List<MySqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Select * from " + TableName + " where ");
			foreach (string key in Entity.FieldValueDic.Keys)
			{
				SqlSB.Append(key + "=@" + key);
				MySqlParameter SP = new MySqlParameter("@" + key, Entity.FieldValueDic[key]);
				ParaList.Add(SP);

			}
			SqlSB.Append("  limit "+pageSize .ToString ());
			return SqlSB.ToString();
		}
		protected internal string GetQueryListSql(out List<SqlParameter> ParaList, int pageSize)
		{
			ParaList = new List<SqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Select top "+pageSize .ToString ()+" * from " + TableName + " where ");
			foreach (string key in Entity.FieldValueDic.Keys)
			{
				SqlSB.Append(key + "=@" + key);
				SqlParameter SP = new SqlParameter("@" + key, Entity.FieldValueDic[key]);
				ParaList.Add(SP);

			}
			return SqlSB.ToString();
		}
		protected internal string GetInsertSql(out List<SqlParameter> ParaList)
		{
			ParaList = new List<SqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("INSERT INTO " + TableName + "(");
			bool isFirst = true;
			string valueStr = "";
			foreach (string key in Entity.FieldValueDic.Keys)
			{
				bool needInsert = true;
				if (FieldList != null && !FieldList.Contains(key))
					needInsert = false;
				if (needInsert)
				{
					if (isFirst)
					{
						SqlSB.Append(key);
						valueStr = valueStr + "@" + key;
						isFirst = false;
					}
					else
					{
						SqlSB.Append("," + key);
						valueStr = valueStr + ",@" + key;
					}
					SqlParameter SP = new SqlParameter("@" + key, Entity.FieldValueDic[key]);
					ParaList.Add(SP);
				}
			}
			if (isFirst)
				return string.Empty;
			else
			{
				SqlSB.Append(") VALUES(" + valueStr + ")");
			}
			return SqlSB.ToString();
		}
		protected internal string GetInsertSql(out List<MySqlParameter> ParaList)
		{
			ParaList = new List<MySqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("INSERT INTO " + TableName + "(");
			bool isFirst = true;
			string valueStr = "";
			foreach (string key in Entity.FieldValueDic.Keys)
			{
				bool needInsert = true;
				if (FieldList != null && !FieldList.Contains(key))
					needInsert = false;
				if (needInsert)
				{
					if (isFirst)
					{
						SqlSB.Append("`"+key+"`");
						valueStr = valueStr + "@" + key;
						isFirst = false;
					}
					else
					{
						SqlSB.Append("," + "`" + key + "`");
						valueStr = valueStr + ",@" + key;
					}
					MySqlParameter SP = new MySqlParameter("@" + key, Entity.FieldValueDic[key]);
					ParaList.Add(SP);
				}
			}
			if (isFirst)
				return string.Empty;
			else
			{
				SqlSB.Append(") VALUES(" + valueStr + ")");
			}
			return SqlSB.ToString();
		}
		protected internal string GetUpdateSql(out List<SqlParameter> ParaList)
		{
			ParaList = new List<SqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Update " + TableName + " set ");
			bool isFirst = true;
			foreach (string key in Entity.FieldValueDic.Keys)
			{
				if (!KeyFieldList.Contains(key))
				{
					if (isFirst)
					{
						SqlSB.Append(key + "=" + "@" + key);
						isFirst = false;
					}
					else
						SqlSB.Append("," + key + "=" + "@" + key);
				}
				SqlParameter SP = new SqlParameter("@" + key, Entity.FieldValueDic[key]);
				ParaList.Add(SP);
			}
			if (isFirst)
				return string.Empty;
			else
			{
				SqlSB.Append(" where ");
				foreach (string KeyField in KeyFieldList)
				{
					if (Entity.FieldValueDic.ContainsKey(KeyField))
					{
						SqlSB.Append(KeyField + "=@" + KeyField);
					}
					else
						throw new ISException("更新数据缺少主键!");
				}

			}
			return SqlSB.ToString();
		}
		protected internal string GetUpdateSql(out List<MySqlParameter> ParaList)
		{
			ParaList = new List<MySqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Update " + TableName + " set ");
			bool isFirst = true;
			foreach (string key in Entity.FieldValueDic.Keys)
			{
				if (!KeyFieldList.Contains(key))
				{
					if (isFirst)
					{
						SqlSB.Append("`"+key + "`=" + "@" + key);
						isFirst = false;
					}
					else
						SqlSB.Append(",`" + key + "`=" + "@" + key);
				}
				MySqlParameter SP = new MySqlParameter("@" + key, Entity.FieldValueDic[key]);
				ParaList.Add(SP);
			}
			if (isFirst)
				return string.Empty;
			else
			{
				SqlSB.Append(" where ");
				isFirst = true;
				foreach (string KeyField in KeyFieldList)
				{
					if (Entity.FieldValueDic.ContainsKey(KeyField))
					{
						if (isFirst)
						{
							isFirst = false;
							SqlSB.Append("`"+KeyField + "`=@" + KeyField);
						}
						else
							SqlSB.Append(" and `"+KeyField + "`=@" + KeyField);
					}
					else
						throw new ISException("更新数据缺少主键!");
				}

			}
			return SqlSB.ToString();
		}
		protected internal string GetDeleteSql(out List<SqlParameter> ParaList)
		{
			ParaList = new List<SqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Delete from " + TableName + " where ");
			foreach (string KeyField in KeyFieldList)
			{
				SqlSB.Append(KeyField + "=@" + KeyField);
				if (Entity.FieldValueDic.ContainsKey(KeyField))
				{
					SqlParameter SP = new SqlParameter("@" + KeyField, Entity.FieldValueDic[KeyField]);
					ParaList.Add(SP);
				}
				else
					throw new ISException("数据库删除数据缺少主键!");
			}
			return SqlSB.ToString();
		}
		protected internal string GetDeleteSql(out List<MySqlParameter> ParaList)
		{
			ParaList = new List<MySqlParameter>();
			StringBuilder SqlSB = new StringBuilder();
			SqlSB.Append("Delete from " + TableName + " where ");
			foreach (string KeyField in KeyFieldList)
			{
				SqlSB.Append(KeyField + "=@" + KeyField);
				if (Entity.FieldValueDic.ContainsKey(KeyField))
				{
					MySqlParameter SP = new MySqlParameter("@" + KeyField, Entity.FieldValueDic[KeyField]);
					ParaList.Add(SP);
				}
				else
					throw new ISException("数据库删除数据缺少主键!");
			}
			return SqlSB.ToString();
		}
		internal string GetCountSql(out List<MySqlParameter> ParaList)
		{
			ParaList = new List<MySqlParameter>();
			StringBuilder SqlSB = new StringBuilder();			
			SqlSB.Append("Select count(1) from " + TableName );
			if (Entity.FieldValueDic.Count > 0)
			{
				SqlSB.Append(" where ");
				foreach (string key in Entity.FieldValueDic.Keys)
				{
					SqlSB.Append(key + "=@" + key);
					MySqlParameter SP = new MySqlParameter("@" + key, Entity.FieldValueDic[key]);
					ParaList.Add(SP);
				}
			}
			return SqlSB.ToString();
		}
		internal string GetCountSql(out List<SqlParameter> ParaList)
		{
			throw new NotImplementedException();
		}
		protected internal virtual  void PreInsert()
		{
		}
		protected internal virtual void PreUpdata()
		{
		}		
	}
}
